﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Xml;

namespace QDT.Payment.AllInPay.Api
{
    /// <summary>
    /// 通联数据包的基类
    /// </summary>
    public class BasePackage
    {
        /// <summary>
        /// 获取一属性值对象在XML文档的显示文本
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        protected string GetField(object e)
        {

            if (null == e)
                return string.Empty;
            else if (e is Enum)
            {
                FieldInfo fi = e.GetType().GetField(e.ToString());
                DescriptionAttribute[] attrs = (DescriptionAttribute[])fi
                                                               .GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return attrs[0].Description;
                else
                    return e.ToString();
            }
            else
                return e.ToString();
        }

        /// <summary>
        /// 创建一个简单带文本的Xml元素，形如<name>value</name>
        /// </summary>
        /// <param name="doc">XML文档对象</param>
        /// <param name="name">元素名</param>
        /// <param name="value">文本内容</param>
        /// <returns></returns>
        protected XmlElement CreateValueElement(XmlDocument doc, string name, string value)
        {
            XmlElement el = doc.CreateElement(name);
            el.AppendChild(doc.CreateTextNode(value));
            return el;
        }
    }
}
